<?php
/*configure*/
error_reporting(E_ALL);
ini_set('display_errors','On');
define('SF_ROOT_DIR',dirname(__FILE__));

if (!defined('PATH_SEPARATOR')) { 
    if (strpos($_ENV['OS'], 'Win')!==false) {
        define('PATH_SEPARATOR', ';'); 
    } else { 
        define('PATH_SEPARATOR', ':');
    } 
}
ini_set('include_path',SF_ROOT_DIR.'/lib'.PATH_SEPARATOR.ini_get('include_path'));
ini_set('default_charset', 'utf-8');

require_once SF_ROOT_DIR.'/lib/functions.php';
require_once SF_ROOT_DIR.'/config.php';

session_name('sid');
if (isset($_COOKIE['sid'])) {
    sf_session_start();
}

cs('dir_view',SF_ROOT_DIR.'/tpls');
cs('dir_www',SF_ROOT_DIR.'/www');

if (!empty($_SERVER["HTTP_ACCEPT_LANGUAGE"])) {
    set_lang(my_locale_accept_from_http($_SERVER["HTTP_ACCEPT_LANGUAGE"]));
} else {
    set_lang('en_US');
}

bindtextdomain("sf", SF_ROOT_DIR."/locale");
bind_textdomain_codeset("sf", 'UTF-8');
textdomain("sf");


if (!cg('www_root_without_host') || !cg('www_host')) {
    $parts=parse_url(cg('www_root'));
    cs('www_root_without_host',$parts['path']);
    cs('www_root_no_host',$parts['path']);
    cs('www_host',$parts['host']);
}

/*debug*/
if (DEBUG_KEY=='SF_DEBUG_KEY') {
    trigger_error("You are using the default DEBUG_KEY, so that anybody else may turn on the debug mode to get your server information. You should define one in config.php which should different from the default one.", E_USER_NOTICE);
}
if (isset($_REQUEST[DEBUG_KEY]) 
    || isset($_COOKIE[DEBUG_KEY])
    || getenv(DEBUG_KEY)!==false
    ) {
    define('DEBUG_MODE',true);
    error_reporting(E_ALL);
    ini_set('display_errors','On');
} else {
    define('DEBUG_MODE',false);
    ini_set('display_errors','Off');
}

/*basic functions*/
function __autoload($class_name) {
    $try_files=array(
            SF_ROOT_DIR.'/lib/'.$class_name.'.php',
            SF_ROOT_DIR.'/core/'.$class_name.'.php',
            );  
    foreach ($try_files as $file) {
        if (is_file($file)) {
            require_once $file; 
        }   
    }
}


/*basic constant and variables*/
if (!defined('REQUEST_TIME')) {
    define('REQUEST_TIME',time());
}

global $bbste,$bbsjk;
$mysql_config=cg('mysql_config');
$bbsjk = new SJK($mysql_config['host'],
        $mysql_config['user'],
        $mysql_config['pass'],
        $mysql_config['database']);
$bbste=new STE;
$bbste->setDir(SF_ROOT_DIR.'/tpls');
cs('www_sf_uri','/'.preg_replace('/^'.preg_quote(cg('www_root_without_host'),'/').'/','',$_SERVER['SCRIPT_NAME']));

/*
if (SfUser::isLogin()) {
    $objUser=new SfUser;
    $lang=$objUser->getMy('lang');
    if (!empty($lang)) {
        set_lang($lang);
    }
} else if (!empty($_COOKIE['lang'])) {
    $lang=$_COOKIE['lang'];
    set_lang($lang);
}
*/
